/**
 * @param {string[]} equations
 * @return {boolean}
 */
var equationsPossible = function (equations) {
  let uf = new UnionFind(26) // 26个字母
  for (let str of equations) {
    if (str[1] == '=') { // 如果是等于号 把对应的位置都放在一起
      let x = str[0]
      let y = str[3]
      uf.union(x.charCodeAt() - 'a'.charCodeAt(), y.charCodeAt() - 'a'.charCodeAt())
    }
  }
  for (let str of equations) {
    if (str[1] == '!') { // 再次遍历 如果是!号 那就看这两个是不是在一起 如果在一起就return false   字符的差值 用charCodeAt方法拿到ascii值在计算
      let x = str[0]
      let y = str[3]
      if (uf.connected(x.charCodeAt() - 'a'.charCodeAt(), y.charCodeAt() - 'a'.charCodeAt())) {
        return false
      }
    }
  }
  return true
};
class UnionFind {
  constructor(n) {
    this.count = n
    this.parent = new Array(n)
    for (let i = 0; i < n; i++) {
      this.parent[i] = i
    }
  }
  union (p, q) {
    let rootP = this.find(p)
    let rootQ = this.find(q)
    if (rootP === rootQ) {
      return
    }
    this.parent[rootP] = rootQ
    this.count--
  }
  connected (p, q) {
    let rootP = this.find(p)
    let rootQ = this.find(q)
    return rootP === rootQ
  }
  find (x) {
    if (this.parent[x] !== x) {
      this.parent[x] = this.find(this.parent[x])
    }
    return this.parent[x]
  }
  getCount () {
    return this.count
  }
}
